package com.showcai.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.showcai.dao.MediaClassifyMapper;
import com.showcai.def.Constant;
import com.showcai.module.Media;
import com.showcai.module.MediaClassify;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.service.MediaClassifyService;
import com.showcai.service.MediaService;
import com.showcai.service.ServiceBase;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by Baggio on 2018-9-11.
 */
@Service
public class MediaClassifyServiceImpl extends ServiceBase implements MediaClassifyService {
@Autowired
MediaClassifyMapper mediaClassifyMapper;
@Autowired
    MediaService mediaService;
    @Override
    public List<MediaClassify> getMediaClassify(Map<String,String> map) throws MyRunTimeExcption {
        return mediaClassifyMapper.selectMediaClassifyList(map);
    }

    @Override
    public PageInfo selectMediaClassifyList(Map<String, String> map) throws MyRunTimeExcption{
        String pageStr = map.get("page");
        String pageSizeStr = map.get("pageSize");
        Integer page = null;
        Integer pageSize = null;
        if (StringUtils.isNotBlank(pageStr) && StringUtils.isNumeric(pageStr)) {
            page = Integer.parseInt(pageStr);
        }
        if (StringUtils.isNotBlank(pageSizeStr) && StringUtils.isNumeric(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }

        if (null == page) {
            page = Constant.PAGENUM;
        }
        if (null == pageSize) {
            pageSize = Constant.PAGESIZENum;
        }
        PageHelper.startPage(page, pageSize);


        List<MediaClassify> mediaClassifyList = mediaClassifyMapper.selectMediaClassifyList(map);

        PageInfo<MediaClassify> pageInfo = new PageInfo<>(mediaClassifyList);

        return pageInfo;
    }

    @Override
    public void modifyMediaClassify(MediaClassify mediaClassify) throws MyRunTimeExcption {
        if(mediaClassify.getTid()==null){
            mediaClassify.setCreateTime(new Date());
           mediaClassify.setParentId(mediaClassify.getParentId()==null?0l:mediaClassify.getParentId());
            mediaClassifyMapper.insertMediaClassify(mediaClassify);
            return;
        }
        mediaClassifyMapper.updateByPrimaryKeySelective(mediaClassify);
    }

    @Override
    public MediaClassify selectMediaClassifyByTid(Map<String, String> map) throws MyRunTimeExcption {
        return mediaClassifyMapper.selectMediaClassifyByTid(map);
    }

    @Override
    public List<MediaClassify> getMediaClassifyTree(Long parentId) throws MyRunTimeExcption {
        List<MediaClassify> mediaClassifylist=getMediaClassifyTrees(parentId,new ArrayList<MediaClassify>());
        return mediaClassifylist;
    }

    private List<MediaClassify> getMediaClassifyTrees(Long parentId, List<MediaClassify> mediaClassifylist) {
        Map map = new HashMap<String, String>();
        map.put("parentId", String.valueOf(parentId));
        mediaClassifylist = getMediaClassify(map);
        for (MediaClassify mediaClassify : mediaClassifylist
                ) {
            List<MediaClassify> mediaClassifylistTemp = getMediaClassifyTrees(mediaClassify.getTid(), new ArrayList<MediaClassify>());
            if (mediaClassifylistTemp.size() > 0) {
                mediaClassify.setMediaClassifyList(mediaClassifylistTemp);
            }
            else{
                map.put("classify",String.valueOf(mediaClassify.getTid()));
                List<Media> mediaList = mediaService.selectMediaList(map);
                mediaClassify.setMediaList(mediaList);
            }
            //TODO 添加MEDIA
        }
        return mediaClassifylist;
    }
}
